package com.library.view;

import com.library.controller.BookController;
import com.library.pojo.dto.book.BookPageQueryDTO;
import com.library.pojo.entity.LibraryBooks;
import com.library.result.PageResult;
import com.library.result.Result;
import com.library.utils.LocalCacheUtil;
import io.yukk1o.easytermui.EasyTermUI;
import io.yukk1o.easytermui.component.Button;
import io.yukk1o.easytermui.component.Panel;
import io.yukk1o.easytermui.component.Text;
import io.yukk1o.easytermui.util.PrintUtils;
import java.util.List;
import java.util.Objects;

/**
 * 菜单界面
 *
 * 包含组件:
 * 顶部：系统标题、统计信息
 * 中间：欢迎信息、功能按钮（通用功能和管理员功能）
 * 底部：系统信息、分割线
 *
 * 交互流程:
 * 1. 显示系统统计信息和功能按钮
 * 2. 点击功能按钮跳转到对应面板
 * 3. 管理员登录时显示额外的管理功能按钮
 */
public class MenuPanel {
    /// 创建EasyTermUI实例
    private final EasyTermUI easyTermUI = new EasyTermUI();

    public MenuPanel() {
        /// 上分割线
        Text topLine = new Text(0, 1, 120, 1,
                "========================================================================================================================");
        /// 创建根面板
        Panel rootPanel = new Panel(0, 0, 120, 40);

        rootPanel.addComponent(topLine);

        /// 系统标题
        Text title = new Text(53, 2, 20, 1, "图书馆管理系统");
        rootPanel.addComponent(title);

        /// 统计信息
        try {
            // 使用pageQuery方法获取图书列表
            BookPageQueryDTO bookPageQueryDTO = new BookPageQueryDTO();
            bookPageQueryDTO.setPageNum(1);
            bookPageQueryDTO.setPageSize(1000); // 获取足够多的图书以进行统计
            /// 创建控制器实例
            BookController bookController = new BookController();
            Result<PageResult> pageResult = bookController.pageQuery(bookPageQueryDTO);

            List<LibraryBooks> books = pageResult.getData() != null && pageResult.getData().getRecords() != null
                    ? (List<LibraryBooks>) pageResult.getData().getRecords() : List.of();

            int inLibraryCount = 0;
            int borrowedCount = 0;
            for (LibraryBooks book : books) {
                if (book.getStock() > 0) {
                    inLibraryCount++;
                } else {
                    borrowedCount++;
                }
            }
            int userCount = 100; // 模拟用户数

            Text statText = new Text(38, 4, 60, 1,
                    String.format("在馆书籍: %d | 借出图书: %d | 注册用户: %d",
                            inLibraryCount, borrowedCount, userCount));
            rootPanel.addComponent(statText);
        } catch (Exception e) {
            Text errorText = new Text(38, 4, 60, 1, "加载统计数据失败");
            rootPanel.addComponent(errorText);
        }

        /// 欢迎信息
        Text welcomeText = new Text(51, 6, 20, 1, "欢迎使用图书馆系统");
        rootPanel.addComponent(welcomeText);

        /// 功能菜单标题
        Text menuTitle = new Text(56, 10, 15, 1, "功能菜单");
        rootPanel.addComponent(menuTitle);

        /// 检查管理员权限 (暂时硬编码为false)
        boolean isAdmin = Objects.equals(LocalCacheUtil.readFromCache("role"), "1");


        /// 通用功能按钮
        Button bookListBtn = new Button(33, 12, " 图书列表 ", (button -> {
            new BookPanel();
            show();
        }));
        rootPanel.addComponent(bookListBtn);

        Button userInfoBtn = new Button(53, 12, " 个人信息 ", (button -> {
            new UserInfoPanel();
            show();
        }));
        rootPanel.addComponent(userInfoBtn);

        Button logoutBtn = new Button(73, 12, " 退   出 ", (button -> {
//            PrintUtils.clearScreen();
//            new LoginAndRegionPanel();
            System.exit(1);
        }));
        rootPanel.addComponent(logoutBtn);

        /// 管理员专属按钮
        if (isAdmin) {
            Text adminLabel = new Text(55, 16, 20, 1, "管理员功能");
            rootPanel.addComponent(adminLabel);

            Button borrowManageBtn = new Button(33, 18, " 借阅管理 ", (button -> {
                new BorrowManagePanel();
                show();
            }));
            rootPanel.addComponent(borrowManageBtn);

            Button bookManageBtn = new Button(53, 18, " 图书管理 ", (button -> {
                new BookManagePanel();
                show();
            }));
            rootPanel.addComponent(bookManageBtn);

            Button userManageBtn = new Button(73, 18, " 用户管理 ", (button -> {
                PrintUtils.clearScreen();
                new UserManagePanel();
                show();
            }));
            rootPanel.addComponent(userManageBtn);
        }

        /// 下分割线
        Text bottomLine = new Text(0, 39, 120, 1, "========================================================================================================================");
        rootPanel.addComponent(bottomLine);

        /// 初始化界面
        PrintUtils.clearScreen();
        easyTermUI.init(rootPanel);
    }

    public void show() {
        PrintUtils.clearScreen();
        easyTermUI.start();
    }
}
